/**
 * @(#) NoticeMediator.as
 *
 * @date		2009. 03. 30.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	공지사항 - Mediator
 * 				공지사항 Presentation Logic을 관리 
 */
package com.skt.common_hr.main.view
{
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.main.ApplicationFacade;
	import com.skt.common_hr.main.model.NoticeProxy;
	import com.skt.common_hr.main.model.vo.NoticeViewVO;
	import com.skt.common_hr.main.view.components.NoticeView;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	

	public class NoticeMediator extends CommonMediator
	{
		public static const NAME:String = "NoticeMediator";
		public var contentIdx:String;
		

		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function NoticeMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
            view.callLater(initView);
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		private function initView():void
 		{
 			view.addEventListener("HIT_NOTICE", onHitNotice);
 		}
 		
 		public function exec(index:String):void
 		{
 			if(index != "") {
	 			view.vsNotice.selectedIndex = 1;
	 			contentIdx = index
				increaseViewCount(parseInt(contentIdx));
 			}
 			else {
	 			view.vsNotice.selectedIndex = 0;
 			}
			var noticeProxy:NoticeProxy = facade.retrieveProxy(NoticeProxy.NAME) as NoticeProxy;
			noticeProxy.viewLoad("99");
 		}
 		
		private function increaseViewCount(index:Number):void
		{
			var noticeProxy:NoticeProxy = facade.retrieveProxy(NoticeProxy.NAME) as NoticeProxy;
			noticeProxy.increaseCount(index);
		}
 		
 		private function onHitNotice(e:Event):void
 		{
 			increaseViewCount(view.vo.CONTENT_IDX);
 		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					ApplicationFacade.LOAD_NOTICE_VIEWLIST_COMPLETE,
					ApplicationFacade.LOAD_NOTICE_VIEWLIST_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case ApplicationFacade.LOAD_NOTICE_VIEWLIST_COMPLETE:
 					//view.vsNotice.selectedIndex = 1;
					onSucceedDataLoad(note.getBody() as NoticeProxy);
					break;
				case ApplicationFacade.LOAD_NOTICE_VIEWLIST_FAILED:
					trace("LoadNoticeViewlistFailed");
					break;
					
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		/**
		 * Proxy에서 DataLoad를 완료한 후 호출됨
		 *     - load된 데이터를 화면에 출력
		 * @param
		 * @return void
		 */
 		private function onSucceedDataLoad(proxy:NoticeProxy):void
 		{
 			var obj:Object = new Object();
 			var idx:Number = acValueCheck(proxy.viewArr, "CONTENT_IDX", contentIdx);
 			
 			if(idx != -1) {
	 			view.vo = proxy.viewArr[idx] as NoticeViewVO;
 			}
 			view._dataProvider = proxy.viewArr;
 		}
 		
 		
 		
		/**
         * ArrayCollection 에서 특정 컬럼의 데이터가 존재하는지 체크
         * @param  acData  비교대상 ArrayCollection
         * @param  Field 비교할 필드명
         * @param  value 비교할 값
         * @return  검색된 첫번째 위치
         */
        public static function acValueCheck(acData:ArrayCollection, Field:String, value:String): Number
        {
            var i: Number = 0;
            var intNum: Number = -1;
            
            for(i = 0; i < acData.length; i++){
                if(acData[i][Field] == value) {
                    intNum = i;
                    break;
                }
            }
            return intNum;
        }
 		
 
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
 		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():NoticeView
        {
            return viewComponent as NoticeView;
        }
	}
}